
//
//  NewsViewController.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "NewsViewController.h"

#import "NewsDetailViewController.h"

#import "OneImageTableViewCell.h"
#import "MoreImageTableViewCell.h"

#import "NewsObject.h"

@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSArray *newsArray;
}

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"新闻中心";
    
    [_myTable registerNib:[UINib nibWithNibName:@"OneImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"one"];
    
    [_myTable registerNib:[UINib nibWithNibName:@"MoreImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"more"];
    
    [self loadData];
}


- (void)loadData
{
    AFHTTPSessionManager *mgr = [NetTool getSessionManager];
    if (mgr)
    {
        [mgr GET:[self getNewsInterfaceUrl] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [NetTool hideIndicator];
            
         //   NSLog(@"-----%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
            if (responseObject)
            {
                NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
                newsArray = [NewsObject mj_objectArrayWithKeyValuesArray:resultDic[@"news_list"]];
                [_myTable reloadData];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [NetTool hideIndicator];
            [Util showAlertWithMessage:LOAD_FAIL confirmBlock:nil];
        }];
    }
}

#pragma makr UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return newsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    NewsObject *obj = newsArray[indexPath.row];
    
    if (obj.images.count == 2) {
        
        MoreImageTableViewCell *moreCell = [tableView dequeueReusableCellWithIdentifier:@"more" forIndexPath:indexPath];
        
        [moreCell.imgVIew1 sd_setImageWithURL:[self getImageUrl:obj.images[0][@"url"]]];
        [moreCell.imgView2 sd_setImageWithURL:[self getImageUrl:obj.images[1][@"url"]]];
        
        
        moreCell.titleLabel.text = obj.news_title;
        moreCell.sourceLabel.text = obj.source;

        
        cell = moreCell;
        
    }
    
    else if (obj.images.count >= 3)
    {
        MoreImageTableViewCell *moreCell = [tableView dequeueReusableCellWithIdentifier:@"more" forIndexPath:indexPath];
        
        [moreCell.imgVIew1 sd_setImageWithURL:[self getImageUrl:obj.images[0][@"url"]]];
        [moreCell.imgView2 sd_setImageWithURL:[self getImageUrl:obj.images[1][@"url"]]];
        [moreCell.imgView3 sd_setImageWithURL:[self getImageUrl:obj.images[2][@"url"]]];
        
        moreCell.titleLabel.text = obj.news_title;
        moreCell.sourceLabel.text = obj.source;
        
        cell = moreCell;
    }
    
    else
    {
        OneImageTableViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"one" forIndexPath:indexPath];
        
        NSDictionary *imageDic = obj.images[0];
        
        [oneCell.imgView sd_setImageWithURL:[self getImageUrl:imageDic[@"url"]]];
        
        
        oneCell.titleLabel.text = obj.news_title;
        oneCell.desLabel.text = obj.intro;
        oneCell.sourceLabel.text = obj.source;
        
        
        cell = oneCell;
    }
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailViewController *detailVC = [[NewsDetailViewController alloc]init];
    
    detailVC.obj = newsArray[indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (NSURL *)getImageUrl:(NSString *)url
{
    return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVER_URL,url]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
